package com.zswl.decorate.api;


import com.zswl.decorate.App;
import com.zswl.decorate.util.LogUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okhttp3.internal.ws.WebSocketProtocol;
import okio.ByteString;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Administrator on 2018/6/9 0009.
 */

public class ApiService {
    private static Api api;
    private static final int DEFAULT_TIMEOUT = 20;
    private final OkHttpClient okHttpClient;
    private OkHttpClient socketClient;

    private ApiService() {
        File httpCacheDirectory = null;
        Cache cache = null;
        //缓存地址
        if (httpCacheDirectory == null) {
            httpCacheDirectory = new File(App.getAppInstance().getCacheDir(), "app_cache");
        }

        try {
            if (cache == null) {
                cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
            }
        } catch (Exception e) {
            LogUtil.d("OKHttp Could not create http cache", e.toString());
        }
        //okhttp创建了
        //                .addNetworkInterceptor(new CaheInterceptor(App.getAppInstance()))
//失败重连
        okHttpClient = new OkHttpClient.Builder()
                .cache(cache)
                .addInterceptor(new CaheInterceptor(App.getAppInstance()))
//                .addNetworkInterceptor(new CaheInterceptor(App.getAppInstance()))
                .addInterceptor(new UserIdInterceptor())
                .addInterceptor(new LogInterceptor())
                .retryOnConnectionFailure(true)//失败重连
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        api = new Retrofit.Builder().baseUrl(Api.HOST)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build().create(Api.class);


    }

    private static class Holder {
        private static ApiService INSTANCE = new ApiService();
    }

    public static ApiService getInstance() {
        return Holder.INSTANCE;
    }

    public Api getApi() {
        return api;
    }

    public WebSocket getSocket(String url, WebSocketListener listener) {
        if (socketClient == null)
            socketClient = new OkHttpClient.Builder()
                    .retryOnConnectionFailure(true)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        socketClient.dispatcher().cancelAll();
        WebSocket webSocket = socketClient.newWebSocket(request, listener);

        return webSocket;
    }

}
